package com.iotechn.unimall.admin.api.distribution;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.biz.service.distribution.DistributionBizService;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.component.CacheComponent;
import com.iotechn.unimall.data.domain.CategoryDO;
import com.iotechn.unimall.data.domain.DistributionStrategyDO;
import com.iotechn.unimall.data.domain.SpuDO;
import com.iotechn.unimall.data.enums.DistributionStrategyType;
import com.iotechn.unimall.data.mapper.CategoryMapper;
import com.iotechn.unimall.data.mapper.DistributionStrategyMapper;
import com.iotechn.unimall.data.mapper.SpuMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: rize
 * Date: 2019/11/27
 * Time: 19:28
 */
@Service
public class AdminDistributionStrategyServiceImpl implements AdminDistributionStrategyService {

    @Autowired
    private DistributionStrategyMapper distributionStrategyMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private CacheComponent cacheComponent;

    @Override
    public String create(Integer type, Long typeId, Integer radio, Integer firstRadio, Integer secondRadio, Integer thirdRadio, Integer mergeType, Long adminId) throws ServiceException {
        if (firstRadio + secondRadio + thirdRadio != 1000) {
            throw new AdminServiceException(ExceptionDefinition.DISTRIBUTION_STRATEGY_RADIO_CHECK_FAILED);
        }
        Integer count = distributionStrategyMapper.selectCount(new EntityWrapper<DistributionStrategyDO>().eq("type_id", typeId).eq("type", type));
        if (count > 0) {
            throw new AdminServiceException(ExceptionDefinition.DISTRIBUTION_STRATEGY_HAS_EXIST);
        }
        Date now = new Date();
        DistributionStrategyDO strategyDO = new DistributionStrategyDO();
        strategyDO.setType(type);
        if (type == DistributionStrategyType.ALL.getCode()) {
            strategyDO.setTypeId(0l);
        } else {
            strategyDO.setTypeId(typeId);
        }
        strategyDO.setRadio(radio);
        strategyDO.setFirstRadio(firstRadio);
        strategyDO.setSecondRadio(secondRadio);
        strategyDO.setThirdRadio(thirdRadio);
        strategyDO.setMergeType(mergeType);
        // 获取标题，冗余存储
        if (type == DistributionStrategyType.ALL.getCode()) {
            strategyDO.setTitle("全部商品");
        } else if (type == DistributionStrategyType.CATEGORY.getCode()) {
            CategoryDO categoryDO = categoryMapper.selectById(typeId);
            if (categoryDO.getLevel() != 2) {
                throw new AdminServiceException(ExceptionDefinition.DISTRIBUTION_STRATEGY_NOT_THIRD_CATEGORY);
            }
            strategyDO.setTitle("分类-" + categoryDO.getTitle());
        } else {
            //剩下关联商品
            SpuDO spuDO = spuMapper.selectById(typeId);
            strategyDO.setTitle("商品-" + spuDO.getTitle());
        }
        strategyDO.setGmtUpdate(now);
        strategyDO.setGmtCreate(now);
        if (distributionStrategyMapper.insert(strategyDO) > 0) {
            cacheComponent.delPrefixKey(DistributionBizService.CA_DISTRIBUTION_STRATEGY_SPU);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.DISTRIBUTION_UNKNOWN_EXCEPTION);
    }

    @Override
    public String edit(Long id, Integer radio, Integer firstRadio, Integer secondRadio, Integer thirdRadio, Integer mergeType, Long adminId) throws ServiceException {
        if (firstRadio + secondRadio + thirdRadio != 1000) {
            throw new AdminServiceException(ExceptionDefinition.DISTRIBUTION_STRATEGY_RADIO_CHECK_FAILED);
        }
        Date now = new Date();
        DistributionStrategyDO strategyDO = new DistributionStrategyDO();
        strategyDO.setId(id);
        strategyDO.setRadio(radio);
        strategyDO.setFirstRadio(firstRadio);
        strategyDO.setThirdRadio(thirdRadio);
        strategyDO.setSecondRadio(secondRadio);
        strategyDO.setMergeType(mergeType);
        strategyDO.setGmtUpdate(now);
        if (distributionStrategyMapper.updateById(strategyDO) > 0) {
            cacheComponent.delPrefixKey(DistributionBizService.CA_DISTRIBUTION_STRATEGY_SPU);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.DISTRIBUTION_UNKNOWN_EXCEPTION);
    }

    @Override
    public Page<DistributionStrategyDO> list(Integer page, Integer limit, Integer type, Long adminId) throws ServiceException {
        Wrapper<DistributionStrategyDO> wrapper = new EntityWrapper<>();
        if (type != null) {
            wrapper.eq("type", type);
        }
        List<DistributionStrategyDO> distributionStrategyDOS = distributionStrategyMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = distributionStrategyMapper.selectCount(wrapper);
        return new Page<DistributionStrategyDO>(distributionStrategyDOS, page, limit, count);
    }

    @Override
    public String delete(Long id) throws ServiceException {
        if (distributionStrategyMapper.deleteById(id) > 0) {
            cacheComponent.delPrefixKey(DistributionBizService.CA_DISTRIBUTION_STRATEGY_SPU);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.DISTRIBUTION_UNKNOWN_EXCEPTION);
    }
}
